package com.etogc.groupchat.group.ui;

import android.content.Context;
import android.content.Intent;
import android.media.MediaPlayer;
import android.net.Uri;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
import android.view.WindowManager;
import android.view.inputmethod.InputMethodManager;
import android.widget.ImageView;
import android.widget.MediaController;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.VideoView;

import com.etogc.groupchat.R;
import com.etogc.groupchat.base.BaseActivity;
import com.etogc.groupchat.bean.GroupInfoBean;
import com.etogc.groupchat.group.biz.GroupHttpBiz;
import com.etogc.http.VolleyHttpUtil;
import com.etogc.util.IntentHelper;
import com.etogc.util.YcToastHelper;
import com.etogc.view.LoadDialog;
import com.google.gson.Gson;

import org.json.JSONObject;

public class ConversationActivity extends BaseActivity implements View.OnClickListener {

    private ImageView ivFav;
    private ImageView ivShare;
    private String groupID;
    private RelativeLayout rlView;
    private VideoView mVideoView;

    @Override
    public boolean dispatchTouchEvent(MotionEvent ev) {
        InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
        if (imm != null) {
            imm.hideSoftInputFromWindow(getWindow().getDecorView().getWindowToken(),
                    0);
        }
        return super.dispatchTouchEvent(ev);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_conversation);
        ivFav = findViewById(R.id.ivRight2);
        ivShare = findViewById(R.id.ivRight);
        rlView = findViewById(R.id.rl_view);
        mVideoView = findViewById(R.id.video_view);

        findViewById(R.id.ivBack).setVisibility(View.VISIBLE);
        findViewById(R.id.ivBack).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                IntentHelper.finish(ConversationActivity.this);
            }
        });

        mVideoView.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
            @Override
            public void onCompletion(MediaPlayer mp) {
                YcToastHelper.getInstance()._toast("播放完成了");
            }
        });

        Intent intent = getIntent();
        if (intent == null || intent.getData() == null)
            return;
        groupID = intent.getData().getQueryParameter("targetId");
        if ("100000".equals(groupID)) {
            ivFav.setVisibility(View.GONE);
            ivShare.setVisibility(View.GONE);
        } else {
            requestGroupInfo();
            ivFav.setOnClickListener(this);
            ivShare.setOnClickListener(this);
        }
        String title = intent.getData().getQueryParameter("title");
        ((TextView) findViewById(R.id.tvTitle)).setText(title);
    }

    private void requestGroupInfo() {
        GroupHttpBiz.getGroupInfo(this, groupID, new VolleyHttpUtil.ResponseCallBack() {
            @Override
            public void handleResponse(JSONObject response, int errCode) {
                LoadDialog.dismiss(ConversationActivity.this);
                GroupInfoBean groupInfoBean = new Gson().fromJson(response.toString(), GroupInfoBean.class);
                if (groupInfoBean.getResultCode() == 200) {
                    GroupInfoBean.GroupInfo data = groupInfoBean.getData();
                    String url = data.getGroupinfo().getUrl();
                    if (TextUtils.isEmpty(url)) {
                        rlView.setVisibility(View.GONE);
                    } else {
                        rlView.setVisibility(View.VISIBLE);
                        mVideoView.setMediaController(new MediaController(ConversationActivity.this));
//                        mVideoView.setVideoURI(Uri.parse("http://alcdn.hls.xiaoka.tv/2017427/14b/7b3/Jzq08Sl8BbyELNTo/index.m3u8"));
                        mVideoView.setVideoURI(Uri.parse(url));
                        mVideoView.start();
                    }
                }
            }
        });
    }

    private void requestFav() {
        GroupHttpBiz.addFavGroup(groupID);
    }

    private void jumpMgrGroup() {
        IntentHelper.jump(this, MgrGroupActivity.class, "groupID", groupID);
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {

            case R.id.ivRight2://收藏
                requestFav();
                break;

            case R.id.ivRight:
                jumpMgrGroup();
                break;
        }
    }
}
